// pages/questionResult/index.js
const api = require('./../../config/api')
const request = require('./../../utils/request')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionResultList: [],
    modalVisible: false, // 筛选弹框
    queList: [],
    defaultVal: '全部问卷',
    selectVal: '',
    selectId: '',
    page: 1, // 当前页数
    size: 15, // 每页显示数量
    totalPage: 0, // 总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const patientInfo = wx.getStorageSync('patientInfo')
    this.setData({
      patientInfo,
    })
    this.getQuestionResultList()
    this.getQueList(patientInfo.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  // 获取评估结果
  getQuestionResultList: async function (questionnaireId = '') {
    let { page, size, patientInfo, questionResultList } = this.data
    let patientId = patientInfo.id
    let params
    if (questionnaireId === '') {
      params = { patientId, page, size }
    } else {
      params = { patientId, questionnaireId, page, size }
    }
    const res = await request.get(api.getQuestionList, params)
    if (res.httpStatus === 200) {
      this.setData({
        questionResultList: [...questionResultList, ...res.data.records],
        totalPage: res.data.pages,
      })
    }
  },
  // 获取该患者用来筛选的列表
  getQueList: async function (patientId) {
    const res = await request.get(api.recordListScreen, { patientId })
    if (res.httpStatus === 200) {
      this.setData({
        queList: res.data,
      })
    }
  },
  questionDetail(val) {
    const item = val.currentTarget.dataset.item
    wx.navigateTo({
      url: `/pages/selfAssessment/question/result?id=${item.id}&isResultList=isResult`,
    })
  },
  // 点击筛选--底部弹窗
  handleFilter: async function () {
    this.showModal()
  },
  handleAll: function () {
    const { patientInfo } = this.data
    this.hideModal()
    this.setData({
      selectVal: '全部问卷',
      defaultVal: '全部问卷',
      selectId: '',
      page: 1,
      totalPage: 0,
      questionResultList: [],
    })
    this.getQuestionResultList()
  },
  handleSure: function () {
    const { selectId, selectVal } = this.data
    if (!selectVal) {
      wx.showToast({
        title: '请选择！',
        icon: 'none',
      })
      return
    }
    this.setData({
      defaultVal: selectVal,
    })
    this.hideModal()
    this.getQuestionResultList(selectId)
  },
  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0,
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true,
      selectVal: '',
    })
    setTimeout(
      function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
        })
      }.bind(this),
      10
    )
  },
  //隐藏对话框
  hideModal: function () {
    const { patientInfo, selectId } = this.data
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
      delay: 0,
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      page: 1,
      totalPage: 0,
      questionResultList: [],
    })
    setTimeout(
      function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export(),
          showModalStatus: false,
        })
      }.bind(this),
      200
    )
  },
  // 点击筛选面板选项
  handleSelect(val) {
    const item = val.currentTarget.dataset.key
    this.setData({
      selectVal: item.questionnaireName,
      // defaultVal: item.questionnaireName,
      selectId: item.questionnaireId,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, totalPage, selectId } = this.data
    if (page === totalPage) {
      return
    }
    page += 1
    this.setData({
      page: page,
    })
    this.getQuestionResultList(selectId)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
